package com.itwn.portal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwn.common.constant.RedisConst;
import com.itwn.portal.model.entity.Book;
import com.itwn.portal.service.BookService;
import com.itwn.portal.mapper.BookMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
* @author 61073
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2024-12-06 15:48:42
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{

    @Cacheable(cacheNames = RedisConst.BOOK_DETAIL, key = "#root.methodName +':'+ #id")
    @Override
    public Book findById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据类别分页查询
     */
    @Cacheable(cacheNames = RedisConst.BOOK_LIST, keyGenerator = "keyGenerator")
    @Override
    public IPage<Book> findByType(Integer pageNum, Integer pageSize, Integer typeId) {
        return baseMapper.selectPage(
                new Page<>(pageNum,pageSize),
                Wrappers.lambdaQuery(Book.class)
                        .eq(Book::getTypeId,typeId)  // 类别条件
                        .orderByDesc(Book::getUpdateTime)
        );
    }

    /**
     * 根据销量降序分页查询
     */
    @Cacheable(cacheNames = RedisConst.BOOK_LIST, keyGenerator = "keyGenerator")
    @Override
    public IPage<Book> findTopSales(Integer pageNum, Integer pageSize) {
        return baseMapper.selectPage(
                new Page<>(pageNum,pageSize),
                Wrappers.lambdaQuery(Book.class)
                        .orderByDesc(Book::getBuyCount)
        );
    }
}




